//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2023-06-26  10-53-22 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2023-06-26  10-53-22 
//===============================





using UnityEngine;

namespace ZS.U3D.BASE.Runtime.Window
{
    public enum UIWindowType 
	{ 
		Normal,
		Fixed,
		Pop,
		Toast
	}
	public abstract class UIWindow
	{

		public string WindowName { private set; get; }

		public UIWindowType UIWindowType { protected set; get; }
		public string SkinPath { protected set; get; }


		public float CanvasScaleFactor
		{
			get {
				return WindowManager.CanvasScaleFactor;
			}
		}

		public float DCanvasScaleFactor
		{
			get
			{
				return WindowManager.DCanvasScaleFactor;
			}
		}

		public Transform Parent { protected set; get; }
		public GameObject Skin { protected set; get; }
		public CanvasGroup CanvasGroup { protected set; get; }
		public Transform SkinTransform { get { return Skin.transform; } }
		private RectTransform _skinRectTransForrm;
		public RectTransform SkinRectTransFrom
		{
			get
			{
				if (_skinRectTransForrm == null)
				{
					_skinRectTransForrm = Skin.GetComponent<RectTransform>();
				}
				return _skinRectTransForrm;
			}
		}

		private object[] _userDatas;

		/// <summary>
		/// 自定义数据
		/// </summary>
		public object UserData
		{
			get
			{
				if (_userDatas != null && _userDatas.Length >= 1)
					return _userDatas[0];
				else
					return null;
			}
		}

		/// <summary>
		/// 自定义数据集
		/// </summary>
		public object[] UserDatas
		{
			get { return _userDatas; }
		}

		public int Depth
		{
			get; set;
		}

		/// <summary>
		/// 窗口可见性
		/// </summary>
		public bool ActiveSelf { private set; get; } = false;


		public virtual void SetActive(bool active) {
			if (ActiveSelf == active)
				return;
			if (active)
			{
				ProtectEnable();
			}
			else
			{
				ProtectDisable();
			}
		}


		private bool _isCreate = false;

		/// <summary>
		/// 窗口创建
		/// </summary>
		protected abstract void OnAwake();

		protected virtual void OnLoadSkin()
		{

		}

		protected abstract void OnStart();

		protected virtual void OnEnable() { }

		public virtual void OnUpdate() { }

		protected virtual void OnDisable() { }

		protected virtual void OnDestroy() { }



		internal void Init(string name, Transform parent)
		{
			WindowName = name;
			Parent = parent;
		}

		internal void SetUserDatas(params object[] userDatas) {
			_userDatas = userDatas;
		}

		internal void InternalCreate() {
			if (_isCreate == false)
			{
				_isCreate = true;
				OnAwake();
			}
		}


		internal void InternalLoad()
		{
			OnLoadSkin();
			if (Skin == null)
			{
				LoadSkinDefault();
			}


			CanvasGroup = Skin.GetComponent<CanvasGroup>();
			if (CanvasGroup == null)
			{
				CanvasGroup = Skin.AddComponent<CanvasGroup>();
			}

			OnStart();
		}

		private void LoadSkinDefault()
		{
			Skin = (GameObject)Object.Instantiate(Resources.Load(SkinPath), Parent);
		}

		protected void ProtectEnable() {
			ActiveSelf = true;
			CanvasGroup.alpha = 1;
			CanvasGroup.blocksRaycasts = true;  //可交互性
			OnEnable();
		}

		protected void ProtectDisable() {
			ActiveSelf = false;
			CanvasGroup.alpha = 0;
			CanvasGroup.blocksRaycasts = false;  //可交互性
			OnDisable();
		}

		internal void InternalDestroy() 
		{
			Object.Destroy(Skin);
			OnDestroy();
		}

	}
}
